# 获取所有CPP文件
macro(glob_recurse_all_cpp_files folder_list variable_name)
  # 清空目标变量
  set(${variable_name} "")

  # 遍历文件夹列表
  foreach(folder ${folder_list})
    # 搜索当前文件夹及其子文件夹中的所有 .cpp 文件
    file(GLOB_RECURSE tmp_files ${folder}/*.cpp)

    # 将找到的文件追加到目标变量中
    list(APPEND ${variable_name} ${tmp_files})
  endforeach()
endmacro()

# 添加子项目
macro(add_subdir SUBDIR)
  if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${SUBDIR}/CMakeLists.txt")
    add_subdirectory(${SUBDIR})
  endif()
endmacro()


macro(CHECK_VERSION)
  # 获取主CMakeLists.txt的项目版本
  set(PROJECT_VERSION_VALUE ${CMAKE_PROJECT_VERSION})
  
  if(PROJECT_VERSION_VALUE)
    # 检查并更新 Resource\version.rc 文件
    set(VERSION_RC_FILE "${CMAKE_SOURCE_DIR}/Resource/version.rc")
    if(EXISTS ${VERSION_RC_FILE})
      file(READ ${VERSION_RC_FILE} VERSION_RC_CONTENT)
      
      # 将版本号转换为逗号分隔的格式 (例如: 0.6.9 -> 0,6,9,0)
      string(REGEX MATCHALL "[0-9]+" VERSION_PARTS ${PROJECT_VERSION_VALUE})
      list(LENGTH VERSION_PARTS VERSION_PARTS_COUNT)
      # 确保有4个数字
      while(VERSION_PARTS_COUNT LESS 4)
        list(APPEND VERSION_PARTS "0")
        math(EXPR VERSION_PARTS_COUNT "${VERSION_PARTS_COUNT} + 1")
      endwhile()
      string(JOIN "," VERSION_COMMA ${VERSION_PARTS})
      
      # 检查是否需要更新 FileVersion 和 ProductVersion
      set(VERSION_UPDATED FALSE)
      
      # 更新 FILEVERSION
      string(REGEX REPLACE "FILEVERSION[ \t]+[0-9,]+" "FILEVERSION ${VERSION_COMMA}" VERSION_RC_CONTENT_NEW "${VERSION_RC_CONTENT}")
      if(NOT "${VERSION_RC_CONTENT_NEW}" STREQUAL "${VERSION_RC_CONTENT}")
        set(VERSION_UPDATED TRUE)
        set(VERSION_RC_CONTENT "${VERSION_RC_CONTENT_NEW}")
        message(STATUS "Updated FILEVERSION to ${VERSION_COMMA}")
      endif()
      
      # 更新 PRODUCTVERSION
      string(REGEX REPLACE "PRODUCTVERSION[ \t]+[0-9,]+" "PRODUCTVERSION ${VERSION_COMMA}" VERSION_RC_CONTENT_NEW "${VERSION_RC_CONTENT}")
      if(NOT "${VERSION_RC_CONTENT_NEW}" STREQUAL "${VERSION_RC_CONTENT}")
        set(VERSION_UPDATED TRUE)
        set(VERSION_RC_CONTENT "${VERSION_RC_CONTENT_NEW}")
        message(STATUS "Updated PRODUCTVERSION to ${VERSION_COMMA}")
      endif()
      
      # 更新 FileVersion 字符串值
      string(REGEX REPLACE "\"FileVersion\",[ \t]*\"[^\"]*\"" "\"FileVersion\", \"${PROJECT_VERSION_VALUE}.0\"" VERSION_RC_CONTENT_NEW "${VERSION_RC_CONTENT}")
      if(NOT "${VERSION_RC_CONTENT_NEW}" STREQUAL "${VERSION_RC_CONTENT}")
        set(VERSION_UPDATED TRUE)
        set(VERSION_RC_CONTENT "${VERSION_RC_CONTENT_NEW}")
        message(STATUS "Updated FileVersion string to ${PROJECT_VERSION_VALUE}.0")
      endif()
      
      # 更新 ProductVersion 字符串值
      string(REGEX REPLACE "\"ProductVersion\",[ \t]*\"[^\"]*\"" "\"ProductVersion\", \"${PROJECT_VERSION_VALUE}.0\"" VERSION_RC_CONTENT_NEW "${VERSION_RC_CONTENT}")
      if(NOT "${VERSION_RC_CONTENT_NEW}" STREQUAL "${VERSION_RC_CONTENT}")
        set(VERSION_UPDATED TRUE)
        set(VERSION_RC_CONTENT "${VERSION_RC_CONTENT_NEW}")
        message(STATUS "Updated ProductVersion string to ${PROJECT_VERSION_VALUE}.0")
      endif()
      
      if(VERSION_UPDATED)
        file(WRITE ${VERSION_RC_FILE} "${VERSION_RC_CONTENT}")
        message(STATUS "Updated version.rc file with version ${PROJECT_VERSION_VALUE}")
      else()
        message(STATUS "version.rc is already up to date")
      endif()
    else()
      message(WARNING "version.rc file not found at ${VERSION_RC_FILE}")
    endif()
    
    # 检查并更新 SGFem\Main\ProgramInfo.h 文件
    set(PROGRAM_INFO_FILE "${CMAKE_SOURCE_DIR}/SGFem/Main/ProgramInfo.h")
    if(EXISTS ${PROGRAM_INFO_FILE})
      file(READ ${PROGRAM_INFO_FILE} PROGRAM_INFO_CONTENT)
      
      # 检查当前版本是否匹配
      string(REGEX MATCH "const static std::string VERSION = \"v?([^\"]+)\"" VERSION_MATCH "${PROGRAM_INFO_CONTENT}")
      if(VERSION_MATCH)
        set(CURRENT_VERSION "${CMAKE_MATCH_1}")
        # 移除可能的 'v' 前缀进行比较
        string(REGEX REPLACE "^v" "" CURRENT_VERSION_CLEAN "${CURRENT_VERSION}")
        
        if(NOT "${CURRENT_VERSION_CLEAN}" STREQUAL "${PROJECT_VERSION_VALUE}")
          # 更新版本号，保持原有的格式（有v前缀则保持）
          if(CURRENT_VERSION MATCHES "^v")
            set(NEW_VERSION_STRING "v${PROJECT_VERSION_VALUE}")
          else()
            set(NEW_VERSION_STRING "${PROJECT_VERSION_VALUE}")
          endif()
          
          string(REGEX REPLACE "const static std::string VERSION = \"[^\"]*\"" 
                 "const static std::string VERSION = \"${NEW_VERSION_STRING}\"" 
                 PROGRAM_INFO_CONTENT_NEW "${PROGRAM_INFO_CONTENT}")
          
          file(WRITE ${PROGRAM_INFO_FILE} "${PROGRAM_INFO_CONTENT_NEW}")
          message(STATUS "Updated ProgramInfo.h file with version ${NEW_VERSION_STRING}")
        else()
          message(STATUS "ProgramInfo.h is already up to date")
        endif()
      else()
        message(WARNING "Could not find VERSION definition in ProgramInfo.h")
      endif()
    else()
      message(WARNING "ProgramInfo.h file not found at ${PROGRAM_INFO_FILE}")
    endif()
  else()
    message(WARNING "Project version not found. Please set VERSION in your project() command.")
  endif()
endmacro(CHECK_VERSION)
